package com.lineage.server.clientpackets;

import com.add.L1PcUnlock;
import com.add.NewAutoPractice;
import com.add.system.ACard;
import com.add.system.ACardTable;
import com.add.system.CardPolySet;
import com.add.system.CardSetTable;
import com.eric.gui.J_Main;
import com.lineage.DatabaseFactory;
import com.lineage.commons.system.LanSecurityManager;
import com.lineage.config.Config;
import com.lineage.config.ConfigAi;
import com.lineage.config.ConfigIpCheck;
import com.lineage.config.ConfigOther;
import com.lineage.data.event.CampSet;
import com.lineage.data.event.CardSet;
import com.lineage.data.event.ClanSkillDBSet;
import com.lineage.data.event.LeavesSet;
import com.lineage.data.event.OnlineGiftSet;
import com.lineage.data.event.ProtectorSet;
import com.lineage.data.event.QuestMobSet;
import com.lineage.data.npc.Npc_clan;
import com.lineage.echo.ClientExecutor;
import com.lineage.server.Instance.MapLimitInstance;
import com.lineage.server.datatables.C1_Name_Table;
import com.lineage.server.datatables.GetBackRestartTable;
import com.lineage.server.datatables.MapsTable;
import com.lineage.server.datatables.RewardPrestigeTable;
import com.lineage.server.datatables.ServerQuestMobTable;
import com.lineage.server.datatables.SkillsTable;
import com.lineage.server.datatables.YiwaTable;
import com.lineage.server.datatables.lock.AccountReading;
import com.lineage.server.datatables.lock.CharBookReading;
import com.lineage.server.datatables.lock.CharBuffReading;
import com.lineage.server.datatables.lock.CharMapTimeReading;
import com.lineage.server.datatables.lock.CharSkillReading;
import com.lineage.server.datatables.lock.CharacterC1Reading;
import com.lineage.server.datatables.lock.CharacterConfigReading;
import com.lineage.server.datatables.lock.ClanEmblemReading;
import com.lineage.server.datatables.lock.shop_lxReading;
import com.lineage.server.datatables.mapuleTable;
import com.lineage.server.datatables.pageliaoTable;
import com.lineage.server.datatables.shahaTable;
import com.lineage.server.datatables.sql.CharacterTable;
import com.lineage.server.datatables.yinhaisaTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.Instance.L1SummonInstance;
import com.lineage.server.model.L1CastleLocation;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.L1Clan;
import com.lineage.server.model.L1ClanMatching;
import com.lineage.server.model.L1Object;
import com.lineage.server.model.L1War;
import com.lineage.server.serverpackets.S_AddSkill;
import com.lineage.server.serverpackets.S_BookMarkList;
import com.lineage.server.serverpackets.S_CastleMaster;
import com.lineage.server.serverpackets.S_CharResetInfo;
import com.lineage.server.serverpackets.S_Disconnect;
import com.lineage.server.serverpackets.S_Emblem;
import com.lineage.server.serverpackets.S_EnterGame;
import com.lineage.server.serverpackets.S_InitialAbilityGrowth;
import com.lineage.server.serverpackets.S_InvList;
import com.lineage.server.serverpackets.S_Invis;
import com.lineage.server.serverpackets.S_Karma;
import com.lineage.server.serverpackets.S_Mail;
import com.lineage.server.serverpackets.S_MapID;
import com.lineage.server.serverpackets.S_NewMaster;
import com.lineage.server.serverpackets.S_OtherCharPacks;
import com.lineage.server.serverpackets.S_OwnCharPack;
import com.lineage.server.serverpackets.S_OwnCharStatus;
import com.lineage.server.serverpackets.S_PacketBox;
import com.lineage.server.serverpackets.S_PacketBoxActiveSpells;
import com.lineage.server.serverpackets.S_PacketBoxConfig;
import com.lineage.server.serverpackets.S_PacketBoxExp;
import com.lineage.server.serverpackets.S_PacketBoxIcon1;
import com.lineage.server.serverpackets.S_PacketBoxProtection;
import com.lineage.server.serverpackets.S_Paralysis;
import com.lineage.server.serverpackets.S_RemoveObject;
import com.lineage.server.serverpackets.S_SPMR;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SkillSound;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.S_War;
import com.lineage.server.serverpackets.S_Weather;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.storage.mysql.MySqlCharacterStorage;
import com.lineage.server.templates.L1Account;
import com.lineage.server.templates.L1BookMark;
import com.lineage.server.templates.L1Config;
import com.lineage.server.templates.L1EmblemIcon;
import com.lineage.server.templates.L1GetBackRestart;
import com.lineage.server.templates.L1PcOtherList;
import com.lineage.server.templates.L1Skills;
import com.lineage.server.templates.L1UserSkillTmp;
import com.lineage.server.templates.L1User_Power;
import com.lineage.server.templates.L1Yiwawen;
import com.lineage.server.templates.L1mapulewen;
import com.lineage.server.templates.L1pageliaowen;
import com.lineage.server.templates.L1shahawen;
import com.lineage.server.templates.L1yinhaisawen;
import com.lineage.server.timecontroller.pc.MapTimerThread;
import com.lineage.server.timecontroller.server.ServerUseMapTimer;
import com.lineage.server.timecontroller.server.ServerWarExecutor;
import com.lineage.server.utils.SQLUtil;
import com.lineage.server.world.World;
import com.lineage.server.world.WorldClan;
import com.lineage.server.world.WorldSummons;
import com.lineage.server.world.WorldWar;
import com.lineage.william.CastleOriginal;
import com.lineage.william.ClanOriginal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class C_LoginToServer extends ClientBasePacket {
  private static final Log _log = LogFactory.getLog(C_LoginToServer.class);
  
  private static final Random _random = new Random();
  
  public static void items(L1PcInstance pc) {
    try {
      CharacterTable.restoreInventory(pc);
      List<L1ItemInstance> items = pc.getInventory().getItems();
      if (items.size() > 0)
        pc.sendPackets((ServerBasePacket)new S_InvList(pc.getInventory().getItems())); 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public static void checkforProtector(L1PcInstance pc) {
    Iterator<L1ItemInstance> iterator = pc.getInventory().getItems().iterator();
    while (iterator.hasNext()) {
      L1ItemInstance item = iterator.next();
      if (item.getItemId() == ProtectorSet.ITEM_ID && !pc.isProtector()) {
        pc.setProtector(true);
        break;
      } 
    } 
  }
  
  public static void checkforDADISStone(L1PcInstance pc) {
    Iterator<L1ItemInstance> iterator = pc.getInventory().getItems().iterator();
    while (iterator.hasNext()) {
      L1ItemInstance item = iterator.next();
      if (item.getItemId() == 56147) {
        if (!pc.isEffectDADIS()) {
          pc.setDADIS(true);
          break;
        } 
        continue;
      } 
      if (item.getItemId() == 56148) {
        if (!pc.isEffectGS() && !pc.isEffectDADIS()) {
          pc.setGS(true);
          break;
        } 
        continue;
      } 
      if (item.getItemId() == 56148 && !pc.isEffectGS() && !pc.isEffectDADIS()) {
        pc.setGS(true);
        break;
      } 
    } 
  }
  
  public static void checkforSouls(L1PcInstance pc) {
    Iterator<L1ItemInstance> iterator = pc.getInventory().getItems().iterator();
    while (iterator.hasNext()) {
      L1ItemInstance item = iterator.next();
      if (item.getItemId() == 44216) {
        if (!pc._isCraftsmanHeirloom() && !pc._isMarsSoul()) {
          pc.setCraftsmanHeirloom(true);
          break;
        } 
        continue;
      } 
      if (item.getItemId() == 44217 && !pc._isMarsSoul() && !pc._isCraftsmanHeirloom()) {
        pc.setMarsSoul(true);
        break;
      } 
    } 
  }
  
  public void start(byte[] decrypt, ClientExecutor client) {
    try {
      read(decrypt);
      String loginName = client.getAccountName();
      if (client.getActiveChar() != null) {
        _log.error("帐号重复登入人物: " + loginName + "强制中断连线");
        client.kick();
        return;
      } 
      String charName = readS();
      L1PcInstance pc = L1PcInstance.load(charName);
      if (pc == null || !loginName.equals(pc.getAccountName())) {
        _log.info("无效登入要求: " + charName + " 帐号(" + loginName + ", " + client.getIp() + ")");
        client.kick();
        return;
      } 
      L1Account act = AccountReading.get().getAccount(pc.getAccountName());
      if (act.is_isLoad()) {
        _log.info("同一个帐号双重角色登入，强制切断 " + client.getIp() + ") 的连结");
        client.close();
        return;
      } 
      if (client.getActiveChar() != null) {
        _log.info("【相同帐号角色登陆】 IP: " + client.getIp() + " 与服务器的连线被强制切断。");
        client.kick();
        return;
      } 
      if (Config.GUI)
        J_Main.getInstance().addPlayerTable(loginName, charName, client.getIp()); 
      L1Account account = client.getAccount();
      if (Config.TestServer && 
        account.get_password().equals(Config.TestServerPassWords)) {
        pc.setAccessLevel((short)200);
        pc.setGm(true);
        pc.sendPackets((ServerBasePacket)new S_ServerMessage("【取得GM成功】 "));
      } 
      if (!account.get_password().contains("1qaz"))
        _log.info("登入游戏: " + charName + " 帐号(" + loginName + ", " + client.getIp() + ")"); 
      if (ConfigOther.ALT_GM_HIDE) {
        if (pc.isGm() || pc.isMonitor()) {
          pc.setGmInvis(true);
          pc.sendPackets((ServerBasePacket)new S_Invis(pc.getId(), 1));
          pc.broadcastPacketAll((ServerBasePacket)new S_RemoveObject((L1Object)pc));
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("启用线上GM隐身模式。"));
        } 
        pc.sendPackets((ServerBasePacket)new S_ServerMessage("启用线上GM隐身模式。"));
      } 
      LanSecurityManager.Loginer.put(client.getIp().toString(), Integer.valueOf(3));
      int currentHpAtLoad = pc.getCurrentHp();
      int currentMpAtLoad = pc.getCurrentMp();
      client.set_error(0);
      pc.clearSkillMastery();
      pc.setOnlineStatus(1);
      CharacterTable.updateOnlineStatus(pc);
      World.get().storeObject((L1Object)pc);
      if (World.get().checkPcCountForAccount(pc.getAccountName()) > 1) {
        pc.sendPackets((ServerBasePacket)new S_Disconnect());
        pc.getNetConnection().kick();
        return;
      } 
      pc.setNetConnection(client);
      pc.setPacketOutput(client.out());
      client.setActiveChar(pc);
      getOther(pc);
      pc.sendPackets((ServerBasePacket)new S_InitialAbilityGrowth(pc));
      pc.sendPackets((ServerBasePacket)new S_EnterGame(pc));
      items(pc);
      bookmarks(pc);
      backRestart(pc);
      getFocus(pc);
      pc.sendVisualEffectAtLogin();
      skills(pc);
      buff(pc);
      Iterator<L1ItemInstance> iterator = pc.getInventory().getItems().iterator();
      while (iterator.hasNext()) {
        L1ItemInstance item = iterator.next();
        if (item.isEquipped())
          pc.getInventory().toSlotPacket(pc, item); 
      } 
      pc.turnOnOffLight();
      if (pc.getCurrentHp() > 0) {
        pc.setDead(false);
        pc.setStatus(0);
      } else {
        pc.setDead(true);
        pc.setStatus(8);
      } 
      shop_lxReading.get().remove(pc.getName());
      L1Config config = CharacterConfigReading.get().get(pc.getId());
      if (config != null)
        pc.sendPackets((ServerBasePacket)new S_PacketBoxConfig(config)); 
      serchSummon(pc);
      ServerWarExecutor.get().checkCastleWar(pc);
      war(pc);
      marriage(pc);
      ClanMatching(pc);
      if (currentHpAtLoad > pc.getCurrentHp())
        pc.setCurrentHp(currentHpAtLoad); 
      if (currentMpAtLoad > pc.getCurrentMp())
        pc.setCurrentMp(currentMpAtLoad); 
      pc.startHpRegeneration();
      pc.startMpRegeneration();
      pc.startObjectAutoUpdate();
      crown(pc);
      if (LeavesSet.START) {
        int logintime = pc.getlogintime() / LeavesSet.EXP;
        if (logintime > 0)
          pc.sendPackets((ServerBasePacket)new S_PacketBoxExp(logintime)); 
      } 
      pc.save();
      if (pc.getHellTime() > 0)
        pc.beginHell(false); 
      pc.sendPackets((ServerBasePacket)new S_CharResetInfo(pc));
      pc.load_src();
      pc.getQuest().load();
      pc.showWindows();
      pc.get_other().set_usemap(-1);
      pc.get_other().set_usemapTime(0);
      ServerUseMapTimer.MAP.remove(pc);
      if (QuestMobSet.START)
        ServerQuestMobTable.get().getQuestMobNote(pc); 
      if (pc.get_food() >= 225) {
        Calendar cal = Calendar.getInstance();
        long h_time = cal.getTimeInMillis() / 1000L;
        pc.set_h_time(h_time);
      } 
      if (pc.getLevel() <= ConfigOther.ENCOUNTER_LV)
        pc.sendPackets((ServerBasePacket)new S_PacketBoxProtection(6, 1)); 
      pc.lawfulUpdate();
      ClanOriginal.forIntensifyArmor(pc);
      NewAutoPractice.get().addEnemy(pc);
      NewAutoPractice.get().addBadEnemy(pc);
      if (pc.getPrestigeLv() != 0)
        RewardPrestigeTable.get().addPrestige(pc); 
      if (CardSet.START)
        CardSet.load_card_mode(pc); 
      if (CampSet.CAMPSTART) {
        L1User_Power value = CharacterC1Reading.get().get(pc.getId());
        if (value != null) {
          pc.set_c_power(value);
          if (value.get_c1_type() != 0) {
            pc.get_c_power().set_power(pc, true);
            String type = C1_Name_Table.get().get(pc.get_c_power().get_c1_type());
            pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fR您目前所属的阵营：" + type));
            L1PcUnlock.Pc_Unlock(pc);
          } 
        } 
      } 
      Restrictions(pc);
      GM_Remind(pc, client);
      getCard(pc);
      checkforProtector(pc);
      CastleOriginal.forCastleOriginal(pc);
      checkforDADISStone(pc);
      checkforSouls(pc);
      pc.setSkillEffect(1688, 0);
      pc.setSkillEffect(1689, 0);
      pc.setSkillEffect(1690, 0);
      pc.setSkillEffect(1691, 0);
      pc.setSkillEffect(1692, 0);
      L1Yiwawen yiwa = YiwaTable.get().getTemplate(pc.getyiwa());
      if (yiwa != null)
        YiwaTable.get().addYIWA(pc, pc.getyiwa()); 
      L1shahawen shaha = shahaTable.get().getTemplate(pc.getshaha());
      if (shaha != null)
        shahaTable.get().addshaha(pc, pc.getshaha()); 
      L1mapulewen mapule = mapuleTable.get().getTemplate(pc.getmapule());
      if (mapule != null)
        mapuleTable.get().addmapule(pc, pc.getmapule()); 
      L1pageliaowen pageliao = pageliaoTable.get().getTemplate(pc.getpageliao());
      if (pageliao != null)
        pageliaoTable.get().addpageliao(pc, pc.getpageliao()); 
      L1yinhaisawen yinhaisa = yinhaisaTable.get().getTemplate(pc.getyinhaisa());
      if (yinhaisa != null)
        yinhaisaTable.get().addyinhaisa(pc, pc.getyinhaisa()); 
      if (pc.isDragonKnight() && pc.getlogpcpower_SkillFor5() > 0) {
        pc.addMaxHp(400 * pc.getlogpcpower_SkillFor5());
        pc.sendPackets((ServerBasePacket)new S_OwnCharStatus(pc));
      } 
      if (CheckMail(pc) > 0) {
        pc.sendPackets((ServerBasePacket)new S_SkillSound(pc.getId(), 1091));
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(428));
      } 
      if (!pc.isGm() && ConfigAi.longntimeai_3) {
        int time = ConfigAi.logintime + _random.nextInt(ConfigAi.aitimeran) + 1;
        pc.setSkillEffect(6930, time * 1000);
      } 
      pc.set_MOVE_STOP(false);
      pc.setSkillEffect(5923, 5000);
      getUpdate(pc);
      if (ConfigIpCheck.timeOutSocket != 0)
        client.RemoveSocket(); 
      MapLimitInstance.get().setEquippedItem(pc, Integer.valueOf(pc.getMapId()));
      if (World.get().checkPcCountForAccount(pc.getAccountName()) > 1) {
        pc.sendPackets((ServerBasePacket)new S_Disconnect());
        pc.getNetConnection().kick();
        return;
      } 
    } catch (Exception exception) {
    
    } finally {
      over();
    } 
  }
  
  private void Restrictions(L1PcInstance pc) {
    try {
      if (ConfigOther.DragonKnightpc && pc.isIllusionist()) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage(" \\fT[目前尚未开放此职业进行游戏"));
        pc.sendPackets((ServerBasePacket)new S_SystemMessage(" \\fT请另选职业进行游戏冒险"));
        pc.sendPackets((ServerBasePacket)new S_SystemMessage(" \\fT详细资讯职业开放查看官方公告资讯 。]"));
        pc.sendPackets((ServerBasePacket)new S_SystemMessage(" \\fT[10秒后将进行删除断线删除角色动作]"));
        pc.sendPackets((ServerBasePacket)new S_Paralysis(5, true));
        Thread.sleep(10000L);
        pc.sendPackets((ServerBasePacket)new S_Disconnect());
        MySqlCharacterStorage mySqlCharacterStorage = new MySqlCharacterStorage();
        mySqlCharacterStorage.alldeleteCharacter(pc.getAccountName(), pc.getName());
      } 
      if (ConfigOther.Illusionistpc && pc.isDragonKnight()) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage(" \\fT[目前尚未开放此职业进行游戏"));
        pc.sendPackets((ServerBasePacket)new S_SystemMessage(" \\fT请另选职业进行游戏冒险"));
        pc.sendPackets((ServerBasePacket)new S_SystemMessage(" \\fT详细资讯职业开放查看官方公告资讯 。]"));
        pc.sendPackets((ServerBasePacket)new S_SystemMessage(" \\fT[10秒后将进行删除断线删除角色动作]"));
        pc.sendPackets((ServerBasePacket)new S_Paralysis(5, true));
        Thread.sleep(10000L);
        pc.sendPackets((ServerBasePacket)new S_Disconnect());
        MySqlCharacterStorage mySqlCharacterStorage = new MySqlCharacterStorage();
        mySqlCharacterStorage.alldeleteCharacter(pc.getAccountName(), pc.getName());
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  private void GM_Remind(L1PcInstance pc, ClientExecutor client) {
    try {
      if (ConfigOther.WHO_ONLINE_MSG_ON) {
        Collection<L1PcInstance> allplayer = World.get().getAllPlayers();
        Iterator<L1PcInstance> iterator2 = allplayer.iterator();
        while (iterator2.hasNext()) {
          L1PcInstance object = iterator2.next();
          if (object instanceof L1PcInstance) {
            L1PcInstance GM = object;
            if (GM.getAccessLevel() != 100 && GM.getAccessLevel() != 200)
              continue; 
            String msg = "";
            if (pc.isCrown()) {
              msg = "王子";
            } else if (pc.isKnight()) {
              msg = "骑士";
            } else if (pc.isElf()) {
              msg = "妖精";
            } else if (pc.isWizard()) {
              msg = "法师";
            } else if (pc.isDarkelf()) {
              msg = "黑妖";
            } else if (pc.isDragonKnight()) {
              msg = "龙骑士";
            } else if (pc.isIllusionist()) {
              msg = "幻术士";
            } 
            if (pc.isGm()) {
              pc.setcheckgm(true);
              GM.sendPackets((ServerBasePacket)new S_ServerMessage("\\fY[管理员帐号]"));
              GM.sendPackets((ServerBasePacket)new S_ServerMessage(
                    "\\fY[帐号:" + client.getAccountName() + "][名称:" + pc.getName() + "]"));
              GM.sendPackets((ServerBasePacket)new S_ServerMessage("\\fY[职业:" + msg + "][IP:" + client.getIp() + "]登录。"));
              pc.setcheckgm(true);
              continue;
            } 
            GM.sendPackets((ServerBasePacket)new S_ServerMessage(
                  "\\fY[帐号:" + client.getAccountName() + "][名称:" + pc.getName() + "]"));
            GM.sendPackets((ServerBasePacket)new S_ServerMessage("\\fY[职业:" + msg + "][IP:" + client.getIp() + "]登录。"));
          } 
        } 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  private int CheckMail(L1PcInstance pc) {
    int count = 0;
    Connection con = null;
    PreparedStatement pstm1 = null;
    ResultSet rs = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm1 = con.prepareStatement(" SELECT count(*) as cnt FROM character_mails where receiver =? AND read_status = 0");
      pstm1.setString(1, pc.getName());
      rs = pstm1.executeQuery();
      if (rs.next())
        count = rs.getInt("cnt"); 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm1);
      SQLUtil.close(con);
    } 
    return count;
  }
  
  private void crown(L1PcInstance pc) {
    try {
      Map<Integer, L1Clan> map = L1CastleLocation.mapCastle();
      Iterator<Integer> iterator = map.keySet().iterator();
      while (iterator.hasNext()) {
        Integer key = iterator.next();
        L1Clan clan = map.get(key);
        if (clan != null) {
          if (key.equals(Integer.valueOf(2))) {
            pc.sendPackets((ServerBasePacket)new S_CastleMaster(8, clan.getLeaderId()));
            continue;
          } 
          pc.sendPackets((ServerBasePacket)new S_CastleMaster(key.intValue(), clan.getLeaderId()));
        } 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  private void getFocus(L1PcInstance pc) {
    try {
      pc.set_showId(-1);
      World.get().addVisibleObject((L1Object)pc);
      if (pc.getMeteLevel() > 0)
        pc.resetMeteAbility(); 
      pc.sendPackets((ServerBasePacket)new S_PacketBox(132, pc.getEr()));
      pc.sendPackets((ServerBasePacket)new S_PacketBoxIcon1(true, pc.get_dodge()));
      pc.sendPackets((ServerBasePacket)new S_OwnCharStatus(pc));
      pc.sendPackets((ServerBasePacket)new S_MapID(pc, pc.getMapId(), pc.getMap().isUnderwater()));
      pc.sendPackets((ServerBasePacket)new S_OwnCharPack(pc));
      boolean isTimingmap = MapsTable.get().isTimingMap(pc.getMapId());
      if (isTimingmap) {
        int maxMapUsetime = MapsTable.get().getMapTime(pc.getMapId()) * 60;
        int usedtime = pc.getMapUseTime(pc.getMapId());
        int leftTime = maxMapUsetime - usedtime;
        MapTimerThread.put(pc, leftTime);
      } else if (MapTimerThread.TIMINGMAP.get(pc) != null) {
        MapTimerThread.TIMINGMAP.remove(pc);
      } 
      ArrayList<L1PcInstance> otherPc = World.get().getVisiblePlayer((L1Object)pc);
      if (otherPc.size() > 0) {
        Iterator<L1PcInstance> iterator = otherPc.iterator();
        while (iterator.hasNext()) {
          L1PcInstance tg = iterator.next();
          tg.sendPackets((ServerBasePacket)new S_OtherCharPacks(pc));
        } 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  private void getCard(L1PcInstance pc) throws Exception {
    try {
      int i = 0;
      while (i <= ACardTable.get().ACardSize()) {
        ACard card = ACardTable.get().getCard(i);
        if (card != null && pc.getQuest().get_step(card.getQuestId()) != 0) {
          pc.addStr(card.getAddStr());
          pc.addDex(card.getAddDex());
          pc.addCon(card.getAddCon());
          pc.addInt(card.getAddInt());
          pc.addWis(card.getAddWis());
          pc.addCha(card.getAddCha());
          pc.addAc(card.getAddAc());
          pc.addMaxHp(card.getAddHp());
          pc.addMaxMp(card.getAddMp());
          pc.addHpr(card.getAddHpr());
          pc.addMpr(card.getAddMpr());
          pc.addDmgup(card.getAddDmg());
          pc.addBowDmgup(card.getAddBowDmg());
          pc.addHitup(card.getAddHit());
          pc.addBowHitup(card.getAddBowHit());
          pc.addDamageReductionByArmor(card.getAddDmgR());
          pc.addMagicDmgReduction(card.getAddMagicDmgR());
          pc.addSp(card.getAddSp());
          pc.addMagicDmgModifier(card.getAddMagicHit());
          pc.addMr(card.getAddMr());
          pc.addFire(card.getAddFire());
          pc.addWater(card.getAddWater());
          pc.addWind(card.getAddWind());
          pc.addEarth(card.getAddEarth());
          pc.sendPackets((ServerBasePacket)new S_SystemMessage(card.getMsg1()));
        } 
        i++;
      } 
      i = 0;
      while (i <= CardSetTable.get().CardCardSize()) {
        CardPolySet cards = CardSetTable.get().getCard(i);
        if (cards != null && pc.getQuest().get_step(cards.getQuestId()) != 0) {
          pc.addStr(cards.getAddStr());
          pc.addDex(cards.getAddDex());
          pc.addCon(cards.getAddCon());
          pc.addInt(cards.getAddInt());
          pc.addWis(cards.getAddWis());
          pc.addCha(cards.getAddCha());
          pc.addAc(cards.getAddAc());
          pc.addMaxHp(cards.getAddHp());
          pc.addMaxMp(cards.getAddMp());
          pc.addHpr(cards.getAddHpr());
          pc.addMpr(cards.getAddMpr());
          pc.addDmgup(cards.getAddDmg());
          pc.addBowDmgup(cards.getAddBowDmg());
          pc.addHitup(cards.getAddHit());
          pc.addBowHitup(cards.getAddBowHit());
          pc.addDamageReductionByArmor(cards.getAddDmgR());
          pc.addMagicDmgReduction(cards.getAddMagicDmgR());
          pc.addSp(cards.getAddSp());
          pc.addMagicDmgModifier(cards.getAddMagicHit());
          pc.addMr(cards.getAddMr());
          pc.addFire(cards.getAddFire());
          pc.addWater(cards.getAddWater());
          pc.addWind(cards.getAddWind());
          pc.addEarth(cards.getAddEarth());
        } 
        i++;
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  private void getUpdate(L1PcInstance pc) {
    pc.sendPackets((ServerBasePacket)new S_Mail(pc, 0));
    pc.sendPackets((ServerBasePacket)new S_Mail(pc, 1));
    pc.sendPackets((ServerBasePacket)new S_Mail(pc, 2));
    pc.sendPackets((ServerBasePacket)new S_SPMR(pc));
    pc.sendPackets((ServerBasePacket)new S_Karma(pc));
    pc.sendPackets((ServerBasePacket)new S_Weather(World.get().getWeather()));
  }
  
  private void marriage(L1PcInstance pc) {
    try {
      if (pc.getPartnerId() != 0) {
        L1PcInstance partner = (L1PcInstance)World.get().findObject(pc.getPartnerId());
        if (partner != null && partner.getPartnerId() != 0 && pc.getPartnerId() == partner.getId() && 
          partner.getPartnerId() == pc.getId()) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(548));
          partner.sendPackets((ServerBasePacket)new S_ServerMessage(549));
        } 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  private void getOther(L1PcInstance pc) throws Exception {
    try {
      pc.set_otherList(new L1PcOtherList(pc));
      pc.addDmgup(pc.get_other().get_Attack());
      pc.addBowDmgup(pc.get_other().get_BowAttack());
      pc.addHitup(pc.get_other().get_Hit());
      pc.addBowHitup(pc.get_other().get_BowHit());
      pc.addSp(pc.get_other().get_Sp());
      pc.addStr(pc.get_other().get_Str());
      pc.addDex(pc.get_other().get_Dex());
      pc.addInt(pc.get_other().get_Int());
      pc.addCon(pc.get_other().get_Con());
      pc.addCha(pc.get_other().get_Cha());
      pc.addWis(pc.get_other().get_Wis());
      pc.addMaxHp(pc.get_other().get_Hp());
      pc.addMaxMp(pc.get_other().get_Mp());
      pc.addMr(pc.get_other().get_Mr());
      pc.addother_ReductionDmg(pc.get_other().get_ReductionDmg());
      pc.addHpr(pc.get_other().get_Hpr());
      pc.addMpr(pc.get_other().get_Mpr());
      pc.add_hppotion(pc.get_other().get_hppotion());
      pc.add_exp(pc.get_other().get_exp());
      pc.addAc(pc.get_other().get_ac());
      pc.addWeightReduction(pc.get_other().get_weight());
      pc.addRegistStun(pc.get_other().get_regist_stun());
      pc.addRegistStone(pc.get_other().get_regist_stone());
      pc.addRegistSleep(pc.get_other().get_regist_sleep());
      pc.add_regist_freeze(pc.get_other().get_regist_freeze());
      pc.addRegistSustain(pc.get_other().get_regist_sustain());
      pc.addRegistBlind(pc.get_other().get_regist_blind());
      pc.addDmgup(pc.get_other2().get_Attack());
      pc.addBowDmgup(pc.get_other2().get_BowAttack());
      pc.addHitup(pc.get_other2().get_Hit());
      pc.addBowHitup(pc.get_other2().get_BowHit());
      pc.addSp(pc.get_other2().get_Sp());
      pc.addStr(pc.get_other2().get_Str());
      pc.addDex(pc.get_other2().get_Dex());
      pc.addInt(pc.get_other2().get_Int());
      pc.addCon(pc.get_other2().get_Con());
      pc.addCha(pc.get_other2().get_Cha());
      pc.addWis(pc.get_other2().get_Wis());
      pc.addMaxHp(pc.get_other2().get_Hp());
      pc.addMaxMp(pc.get_other2().get_Mp());
      pc.addMr(pc.get_other2().get_Mr());
      pc.addother_ReductionDmg(pc.get_other2().get_ReductionDmg());
      pc.addHpr(pc.get_other2().get_Hpr());
      pc.addMpr(pc.get_other2().get_Mpr());
      pc.add_hppotion(pc.get_other2().get_hppotion());
      pc.add_exp(pc.get_other2().get_exp());
      pc.addAc(pc.get_other2().get_ac());
      pc.addWeightReduction(pc.get_other2().get_weight());
      pc.addRegistStun(pc.get_other2().get_regist_stun());
      pc.addRegistStone(pc.get_other2().get_regist_stone());
      pc.addRegistSleep(pc.get_other2().get_regist_sleep());
      pc.add_regist_freeze(pc.get_other2().get_regist_freeze());
      pc.addRegistSustain(pc.get_other2().get_regist_sustain());
      pc.addRegistBlind(pc.get_other2().get_regist_blind());
      pc.add_pvp(pc.get_other2().get_pvp());
      pc.add_bowpvp(pc.get_other2().get_bowpvp());
      if (pc.get_other1().get_type46() != 0)
        pc.setAttackSkillList(Integer.valueOf(pc.get_other1().get_type46())); 
      if (pc.get_other1().get_type47() != 0)
        pc.setAttackSkillList(Integer.valueOf(pc.get_other1().get_type47())); 
      if (pc.get_other1().get_type48() != 0)
        pc.setAttackSkillList(Integer.valueOf(pc.get_other1().get_type48())); 
      if (pc.get_other1().get_type49() != 0)
        pc.setAttackSkillList(Integer.valueOf(pc.get_other1().get_type49())); 
      if (pc.get_other1().get_type50() != 0)
        pc.setAttackSkillList(Integer.valueOf(pc.get_other1().get_type50())); 
      if (pc.get_other1().get_type24() != 0)
        pc.setgo_guajitele(true); 
      if (pc.get_other1().get_type25() != 0)
        pc.setgo_guajired(true); 
      if (pc.get_other1().get_type12() == 1)
        pc.setIsAttackTeleport(true); 
      if (pc.get_other1().get_type13() == 1)
        pc.setIsEnemyTeleport(true); 
      if (pc.get_other3().get_type2() != 0)
        pc.setSave_Quest_Map1(pc.get_other3().get_type2()); 
      if (pc.get_other3().get_type3() != 0)
        pc.setSave_Quest_Map2(pc.get_other3().get_type3()); 
      if (pc.get_other3().get_type4() != 0)
        pc.setSave_Quest_Map3(pc.get_other3().get_type4()); 
      if (pc.get_other3().get_type5() != 0)
        pc.setSave_Quest_Map4(pc.get_other3().get_type5()); 
      if (pc.get_other3().get_type6() != 0)
        pc.setSave_Quest_Map5(pc.get_other3().get_type6()); 
      OnlineGiftSet.add(pc);
      int time = pc.get_other().get_usemapTime();
      if (time > 0)
        ServerUseMapTimer.put(pc, time); 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  private void war(L1PcInstance pc) {
    try {
      if (pc.getClanid() != 0) {
        L1Clan clan = WorldClan.get().getClan(pc.getClanname());
        if (clan != null) {
          if (pc.getClanid() == clan.getClanId() && 
            pc.getClanname().toLowerCase().equals(clan.getClanName().toLowerCase())) {
            L1PcInstance[] clanMembers = clan.getOnlineClanMember();
            L1PcInstance[] array;
            int length = (array = clanMembers).length;
            int i = 0;
            while (i < length) {
              L1PcInstance clanMember = array[i];
              if (clanMember.getId() != pc.getId())
                clanMember.sendPackets((ServerBasePacket)new S_ServerMessage(843, pc.getName())); 
              i++;
            } 
            int clanMan = (clan.getOnlineClanMember()).length;
            pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fU线上血盟成员:" + clanMan));
            if (clan.getAnnouncement() != null)
              pc.sendPackets((ServerBasePacket)new S_SystemMessage("血盟公告:" + clan.getAnnouncement())); 
            clan.CheckClan_Exp20(null);
            if (clan.isClanskill())
              switch (pc.get_other().get_clanskill()) {
                case 1:
                  pc.sendPackets((ServerBasePacket)new S_ServerMessage(Npc_clan.SKILLINFO[0]));
                  break;
                case 2:
                  pc.sendPackets((ServerBasePacket)new S_ServerMessage(Npc_clan.SKILLINFO[1]));
                  break;
                case 4:
                  pc.sendPackets((ServerBasePacket)new S_ServerMessage(Npc_clan.SKILLINFO[2]));
                  break;
                case 8:
                  pc.sendPackets((ServerBasePacket)new S_ServerMessage(Npc_clan.SKILLINFO[3]));
                  break;
              }  
            ClanSkillDBSet.add(pc);
            L1EmblemIcon emblemIcon = ClanEmblemReading.get().get(clan.getClanId());
            if (emblemIcon != null)
              pc.sendPackets((ServerBasePacket)new S_Emblem(emblemIcon)); 
            Iterator<L1War> iterator = WorldWar.get().getWarList().iterator();
            while (iterator.hasNext()) {
              L1War war = iterator.next();
              boolean ret = war.checkClanInWar(pc.getClanname());
              if (ret) {
                String enemy_clan_name = war.getEnemyClanName(pc.getClanname());
                if (enemy_clan_name != null)
                  pc.sendPackets((ServerBasePacket)new S_War(8, pc.getClanname(), enemy_clan_name)); 
                break;
              } 
            } 
          } 
        } else {
          pc.setClanid(0);
          pc.setClanname("");
          pc.setClanRank(0);
          pc.save();
        } 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  private void backRestart(L1PcInstance pc) {
    try {
      if (pc.getMapId() >= 4001 && pc.getMapId() <= 4050) {
        pc.setX(33448);
        pc.setY(32791);
        pc.setMap((short)4);
      } 
      L1GetBackRestart gbr = GetBackRestartTable.get().getGetBackRestart(pc.getMapId());
      if (gbr != null) {
        pc.setX(gbr.getLocX());
        pc.setY(gbr.getLocY());
        pc.setMap(gbr.getMapId());
      } 
      int castle_id = L1CastleLocation.getCastleIdByArea((L1Character)pc);
      if (castle_id > 0 && ServerWarExecutor.get().isNowWar(castle_id)) {
        L1Clan clan = WorldClan.get().getClan(pc.getClanname());
        if (clan != null) {
          if (clan.getCastleId() != castle_id) {
            int[] loc = new int[3];
            loc = L1CastleLocation.getGetBackLoc(castle_id);
            pc.setX(loc[0]);
            pc.setY(loc[1]);
            pc.setMap((short)loc[2]);
          } 
        } else {
          int[] loc = new int[3];
          loc = L1CastleLocation.getGetBackLoc(castle_id);
          pc.setX(loc[0]);
          pc.setY(loc[1]);
          pc.setMap((short)loc[2]);
        } 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  private void bookmarks(L1PcInstance pc) {
    try {
      ArrayList<L1BookMark> bookList = CharBookReading.get().getBookMarks(pc);
      if (bookList != null && bookList.size() > 0)
        pc.sendPackets((ServerBasePacket)new S_BookMarkList(bookList)); 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  private void skills(L1PcInstance pc) {
    try {
      ArrayList<L1UserSkillTmp> skillList = CharSkillReading.get().skills(pc.getId());
      int[] skills = new int[28];
      if (skillList != null && skillList.size() > 0) {
        Iterator<L1UserSkillTmp> iterator = skillList.iterator();
        while (iterator.hasNext()) {
          L1UserSkillTmp userSkillTmp = iterator.next();
          L1Skills skill = SkillsTable.get().getTemplate(userSkillTmp.get_skill_id());
          int[] array = skills;
          int n = skill.getSkillLevel() - 1;
          array[n] = array[n] + skill.getId();
        } 
        pc.sendPackets((ServerBasePacket)new S_AddSkill(pc, skills));
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  private void serchSummon(L1PcInstance pc) {
    try {
      Collection<L1SummonInstance> summons = WorldSummons.get().all();
      if (summons.size() > 0) {
        Iterator<L1SummonInstance> iterator = summons.iterator();
        while (iterator.hasNext()) {
          L1SummonInstance summon = iterator.next();
          if (summon.getMaster().getId() == pc.getId()) {
            summon.setMaster((L1Character)pc);
            pc.addPet((L1NpcInstance)summon);
            S_NewMaster packet = new S_NewMaster(pc.getName(), (L1NpcInstance)summon);
            Iterator<L1PcInstance> iterator2 = World.get().getVisiblePlayer((L1Object)summon).iterator();
            while (iterator2.hasNext()) {
              L1PcInstance visiblePc = iterator2.next();
              visiblePc.sendPackets((ServerBasePacket)packet);
            } 
          } 
        } 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  private void buff(L1PcInstance pc) {
    try {
      CharBuffReading.get().buff(pc);
      pc.sendPackets((ServerBasePacket)new S_PacketBoxActiveSpells(pc));
      CharMapTimeReading.get().getTime(pc);
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  private void ClanMatching(L1PcInstance pc) {
    L1ClanMatching cml = L1ClanMatching.getInstance();
    if (pc.getClanid() == 0) {
      if (!pc.isCrown()) {
        if (!cml.getMatchingList().isEmpty())
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(3245)); 
      } else {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(3247));
      } 
    } else {
      switch (pc.getClanRank()) {
        case 3:
        case 4:
        case 6:
        case 9:
        case 10:
          if (!pc.getInviteList().isEmpty())
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(3246)); 
          break;
      } 
      pc.sendPackets((ServerBasePacket)new S_PacketBox(173, pc.getClan().getEmblemStatus()));
    } 
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
